package ECMap

import (
	"fmt"
	"math/rand"

	"git.oschina.net/yangdao/GameLib/Shape"
	"git.oschina.net/yangdao/extlib/data_type"
	"git.oschina.net/yangdao/extlib/log"
)

type GameMap struct {
	Shape.RectShape
	Items *data_type.TypVector
}

func (this *GameMap) Init() {
	for i := 0; i < this.Items.Count(); i++ {
		item := this.Items.Nth(i).(*MapItem)
		fmt.Println(item.Slot)
	}
}

func (this *GameMap) OnLoad() {
	for i := 0; i < 1000; i++ {
		genS := &Shape.RectShape{Width: 1, Height: 1}
		newS := this.GetRandomRect(genS)
		newItem := &MapItem{Shape: newS, Slot: 2001}
		this.Items.Conj(newItem)
		fmt.Println(newS)
	}
	log.Log("GameLogic,GameMap", "地图加载成功")
}

func (this *GameMap) Clone() *GameMap {
	newGameMap := &GameMap{
		RectShape: Shape.RectShape{
			Width:  this.Width,
			Height: this.Height,
		},
	}
	newGameMap.Items = data_type.Vector()
	for i := 0; i < this.Items.Count(); i++ {
		newGameMap.AddItem(this.Items.Nth(i).(*MapItem))
	}
	return newGameMap
}

func (this *GameMap) AddItem(mapItem *MapItem) {
	this.Items.Conj(mapItem)
}

func (this *GameMap) GetRandomRect(rectShape *Shape.RectShape) *Shape.RectShape {
	isRet := false
	ri := int64(0)
	for !isRet {
		//rand.Seed(time.Now().UnixNano() + ri*100000)
		rx := rand.Intn(this.Width)
		ry := rand.Intn(this.Height)
		rectShape.X = float32(rx)
		rectShape.Y = float32(ry)
		isRet = true
		/*
			for i := 0; i < this.Items.Count(); i++ {
				mapItem := this.Items.Nth(i).(*MapItem)
				BBox := mapItem.Shape.BBox()
				//fmt.Println("Test", rectShape.X, rectShape.Y, BBox.X, BBox.Y)
				if BBox.TestRect(rectShape) {
					isRet = false
				}
			}*/
		ri++
	}
	return rectShape
}
